<template>
  <div class="login-container">
    <!-- 1、登陆form -->
    <el-form
      :model="loginForm"
      :rules="loginRules"
      ref="loginForm"
      label-width="80px"
    >
      <el-form-item label="用户名" prop="username">
        <el-input v-model="loginForm.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="loginForm.password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('loginForm')"
          >确认</el-button
        >
      </el-form-item>
    </el-form>
    <!-- 2、悬浮按钮 -->
    <floatButton />
    <!-- 3、短信编辑 -->
    <smsEdit />
  </div>
</template>

<script>
import floatButton from "./home/floatButton.vue";
import smsEdit from "./home/smsEdit.vue";
export default {
  name: "Login",
  components: {
    floatButton,
    smsEdit,
  },
  data() {
    return {
      loginForm: {
        username: "",
        password: "",
      },
      loginRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  mounted() {
    console.log("--Login--mounted--");
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 调用模拟登录接口
          this.$http
            .post("/api/login", this.loginForm)
            .then((response) => {
              if (response.code === 200) {
                this.$store.commit("setAuthToken", response.token);
                this.$router.push("/smsEdit");
              } else {
                this.$message.error(response.message);
              }
            })
            .catch((error) => {
              console.error("登录请求出错:", error);
              this.$message.error("登录请求出错，请稍后重试");
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style scoped>
.login-container {
  padding: 20px;
}
</style>
